package xyz.yuanjilu.service.impl;


import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import xyz.yuanjilu.service.MQPublish;

@Component
public class DemoPublish implements MQPublish {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private final static String EXCHANGE_NAME = "ex_publish";  
	@Resource
	ConnectionFactory factory ;

	public void sendMessage(String message) throws Exception {
		Connection connection = factory.createConnection();  
        Channel channel = connection.createChannel(false);  
        // 声明转发器和类型  
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout" );  
        //channel.exchangeBind(EXCHANGE_NAME,"subscribeQueue", "subscribeQueue");
        // 往转发器上发送消息  
        channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());  

        channel.close();  
        connection.close();  
		logger.info(this.toString() + "发布消息" + message);
		/*MessageProperties messageProperties = new MessageProperties();
		Message message2 =new Message(message.getBytes(), messageProperties);
		// 发送消息到消息队列服务器中
		amqpTemplate.send("fanout","subscribeQueue", message2);*/
	}
}
